home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.09 Sep 93 / FrameAnim App ƒ / Main.c < prev    next >
Encoding:
Text File  |  1994-11-06  |  6.8 KB  |  275 lines  |  [TEXT/KAHL]

  1. // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. // • Program:    Main
  3. // • File:        Main.c
  4. // •
  5. // • Copyright © 1993 by Scott B. Steinman, O.D., Ph.D. All Rights Reserved.
  6. // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  7.  
  8. #include "FrameAnim.h"
  9.  
  10. // • ------------------ External Globals ---------------------------------- 
  11.  
  12. extern PicHandle    gFramePICTs[];        // • From Files.c file         
  13.  
  14. // • ------------------ Internal Globals ---------------------------------- 
  15.  
  16. CWindowPtr        gMainWindow;            // • Window for animation display 
  17. GWorldPtr        gFrames[ kMaxFrames ];    // • Offscreen GWorlds for frames 
  18. GDHandle        gDevice;                 // • GDevice of display monitor
  19. PaletteHandle    gPalette;                // • Gray-scale palette 
  20. CTabHandle        gCTable;                // • Gray-scale color table 
  21. EventRecord        gEvent;                    // • Event record for user event handling 
  22. Handle            gMenuBar;                // • Handle to menu bar
  23. Settings        gSettings;                // • Animation settings
  24. Flags            gFlags;                    // • General program flags
  25.  
  26. // • ------------------ Static Functions ---------------------------------- 
  27.  
  28. static void        CheckEnvironment( void );
  29. static void        InitParams( void );
  30. static void        SetMonitorDepth( void );
  31. static void        StartThings( void );
  32.  
  33. // • ------------------ Check Hardware & Software Environment ------------- 
  34.  
  35. static void
  36. CheckEnvironment( void )
  37. // • Confirm proper environment for running program.
  38. // • The program requires the Slot Manager for the VBlank timing and
  39. // • 32-bit QuickDraw, so it will not run on a Mac Plus, SE, SE/30, 
  40. // • Portable or Classic I or II.
  41. {
  42.     long    gestResponse;
  43.     short    version;
  44.     OSErr    gestErr;
  45.  
  46.     gestErr = Gestalt( gestaltVersion, &gestResponse );
  47.     if (!gestErr) {    
  48.     
  49.         // • Ensure that machine is running System 7 or higher. If not, exit.
  50.     
  51.         Gestalt( gestaltSystemVersion, &gestResponse );
  52.         version = HiWord( gestResponse );
  53.         if (gestResponse < 7) {                // • No NuBus slots
  54.             StopAlert( kSystem7AlertID, NullPointer );    
  55.             ExitToShell();
  56.         }
  57.  
  58.         // • Ensure that machine has NuBus slots 
  59.         // • for Slot Manager routines. If not, exit.
  60.  
  61.         Gestalt( gestaltNuBusConnectors, &gestResponse );
  62.         if (gestResponse == 0) {                // • No NuBus slots
  63.             StopAlert( kMachineAlertID, NullPointer );    
  64.             ExitToShell();
  65.         }
  66.  
  67.         // • Check QuickDraw version to ensure that machine has Palette Manager.
  68.         
  69.         Gestalt( gestaltQuickdrawVersion, &gestResponse );
  70.         if (gestResponse == gestaltOriginalQD || gestResponse == gestalt8BitQD) {
  71.              StopAlert( kQD32BitAlertID, NullPointer );    
  72.             ExitToShell();
  73.         }
  74.         
  75.         // • Ensure that machine has FPU.
  76.         
  77.         Gestalt( gestaltFPUType, &gestResponse );
  78.         if (gestResponse == gestaltNoFPU) {
  79.             StopAlert( kFPUAlertID, NullPointer );    
  80.             ExitToShell();
  81.         }
  82.         
  83.         // • Get number of monitors in system.
  84.         
  85.         GetNumMonitors();
  86.     }
  87.     else 
  88.         ExitToShell();
  89. }
  90.  
  91. // •------------------- Set Monitor Color Depth ---------------------------
  92.  
  93. static void
  94. SetMonitorDepth( void )
  95. // • 
  96. // • Changes monitor depth to match desired depth of animation frames.
  97. {
  98.     PixMapHandle    pmh = NullHandle;
  99.     
  100.     gDevice = GetMainDevice();
  101.     if (gSettings.numMonitors == 2 && gSettings.displayMonitor == kSecondary)
  102.         gDevice = GetNextDevice( gDevice );
  103.  
  104.     HLock( (Handle) gDevice );
  105.     pmh = (**gDevice).gdPMap;
  106.     HLock( (Handle) pmh );
  107.     gSettings.currDepth = (**pmh).pixelSize;
  108.     HUnlock( (Handle) pmh );
  109.     HUnlock( (Handle) gDevice );        
  110.     
  111.     if (HasDepth( gDevice, kDepth, 1, 1 ) != 0)
  112.         SetDepth( gDevice, kDepth, 1, 1 );    
  113.     else 
  114.          ErrorHandler( kNoDepthMsg, (char *) NilString, (char *) NilString, 
  115.           (char *) NilString );
  116. }
  117.  
  118. // •------------------- Animation Parameter Set-up Routines ---------------
  119.  
  120. static void
  121. InitParams( void )
  122. // •
  123. // • Used to initialize animation parameters.
  124. {
  125.     short        i;
  126.  
  127.     // • Ensure that pointers and handles are properly initialized to NULL 
  128.  
  129.     gMenuBar = NullHandle;
  130.     gPalette = NullHandle;
  131.     gCTable = NullHandle;
  132.     gMainWindow = NullPointer;
  133.         
  134.     for (i = 0; i < kMaxFrames; i++)
  135.         gFrames[ i ] = NullPointer;     
  136.     for (i = 0; i < kMaxFrames; i++)
  137.         gFramePICTs[ i ] = NullHandle;    
  138.  
  139.     // • Initialize program flags
  140.     
  141.     gFlags.done = false;                    
  142.     gFlags.cancel = false;
  143.     
  144.     // • Set default values for animation parameters
  145.  
  146.     gSettings.numFrames = 20;            // • Total # of frames filmed 
  147.     gSettings.frameDelay = 3;            // • Wait 3 VBlanks between frames. 
  148.     gSettings.frameSize = 190;            // • Frame width (and height) in pixels 
  149.     gSettings.sizeDiff = 3;                // • Target size change of 3 pixels 
  150.     gSettings.bkgndGray = 100;
  151.     gSettings.targetGray = 10;
  152. }
  153.  
  154. // • ------------------ Get Program Ready to Go --------------------------- 
  155.  
  156. static void
  157. StartThings( void )
  158. {
  159.     SetMonitorDepth();
  160.     SetUpWindows();                        
  161.     SetUpMenus();
  162.     InitCursor();
  163.     FlushEvents( everyEvent, 0 );
  164. }
  165.  
  166. // • ------------------ Free As Much Heap Space As Possible --------------- 
  167.  
  168. void
  169. MaximizeHeap( const long bytesNeeded )
  170. // •
  171. // • Get more contiguous heap space.
  172. {
  173.     long    totalBytes, contigBytes;
  174.     
  175.     // • If enough contiguous heap space, exit.
  176.     
  177.     if (MaxBlock() >= bytesNeeded) 
  178.         return;
  179.         
  180.     // • See how much space is possible.
  181.     
  182.     PurgeSpace( &totalBytes, &contigBytes );
  183.     
  184.     // • Free all purgeable, unlocked blocks and compact memory. 
  185.     
  186.     PurgeMem( totalBytes );
  187.     CompactMem( totalBytes );
  188. }
  189.  
  190. // • ------------------ Error Message Printing Routine -------------------- 
  191.  
  192. void
  193. ErrorHandler( const short stringNum, const char *optStr1, const char *optStr2, 
  194.  const char *optStr3 )
  195. // •
  196. // • Displays alert box with error message.
  197. {
  198.     Str255        errorStr;
  199.     
  200.     CleanUp();
  201.     GetIndString( errorStr, kErrorMessageStrsID, stringNum );
  202.     ParamText( errorStr, optStr1, optStr2, optStr3 );
  203.     StopAlert( kErrorAlertID, NullPointer );
  204.     ExitToShell();
  205. }
  206.  
  207. // •------------------- Convert Error Number To PString -------------------
  208.  
  209. void
  210. ErrNumToPstr( const short iNum, char *str )
  211. // •
  212. // • Convert integer error code number to Pascal-format text string.
  213. {
  214.     PtoCstr( str );
  215.     sprintf( str, "%d", iNum );
  216.     CtoPstr( str );
  217. }
  218.  
  219. // • ------------------ Clean Up All Allocated Memory --------------------- 
  220.  
  221. void
  222. CleanUp( void )
  223. // •
  224. // • Deallocate film memory.
  225. {
  226.     short    i;
  227.  
  228.     // • Free any remaining animation frame GWorlds  
  229.     // • and frame Picture record
  230.     
  231.     DisposeFrames();
  232.     DisposeFramePICTs();
  233.  
  234.      // • Free any remaining color records 
  235.                  
  236.      if (gPalette != NullHandle) {
  237.          DisposePalette( gPalette );
  238.          gPalette = NullHandle;    
  239.      }
  240.     if (gCTable != NullHandle) {
  241.         DisposCTable( gCTable );
  242.         gCTable = NullHandle;
  243.     }
  244.                  
  245.      // • Free any remaining windows 
  246.                  
  247.     if (gMainWindow != NullPointer) {
  248.          CloseWindow( gMainWindow );
  249.          gMainWindow = NullPointer;    
  250.      }            
  251.  
  252.     // • Reset monitor depth. 
  253.                 
  254.     SetDepth( gDevice, gSettings.currDepth, 1, 1 );    
  255. }
  256.  
  257. // • ------------------ Main Calling Program ------------------------------ 
  258.  
  259. int
  260. main()
  261. // •
  262. // • Main calling function - 
  263. // • It does initialization, then enters the event-handler loop.
  264. {
  265.     InitManagers();
  266.     CheckEnvironment();
  267.     InitParams();
  268.     if (gSettings.numMonitors == 2)
  269.         MonitorDlg(); 
  270.     StartThings();
  271.  
  272.     MainEventLoop();
  273.     CleanUp();
  274. }
  275.